﻿@using HCMS.Extensions
@using HCMS.Dictionary;
@model HCMS.Entities.Manage.Admin
@{
    Layout = "~/Views/Shared/_Layout_Edit_Pear_Layui.cshtml";

    int roleId = ViewData["roleId"].ToInt(0);
}

<div class="layui-body layui-bg-gray">
    <div class="layui-card">
        <form class="layui-form">
            <input type="hidden" asp-for="Id" />

            <div class="layui-tab layui-tab-card">
                <ul class="layui-tab-title">
                    <li class="layui-this">基础信息</li>
                    <li>登录信息</li>
                    <li>其它</li>
                </ul>

                <div class="layui-tab-content">
                    <!-- 基础信息 -->
                    <div class="layui-tab-item layui-show">

                        <div class="layui-form-item">
                            <div class="layui-form-label">名称</div>
                            <div class="layui-input-inline" style="width:140px;">
                                <input type="text" asp-for="AdminName" lay-verify="required" autocomplete="off" placeholder="请填写名称"
                                       class="layui-input" />
                            </div>

                            <div class="layui-form-label">角色</div>
                            <div class="layui-input-inline" style="width:230px;">
                                <div id="roleClassTree"></div>
                                <input type="hidden" name="roleId" id="roleId" value="@roleId" />
                                <input type="hidden" name="OriginalRoleId" value="@roleId" />
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-form-label">头像</div>
                            <div class="layui-input-inline">
                                <vc:upload-single-img field-name="Logo" img-path="@Model.Logo"></vc:upload-single-img>
                            </div>

                            <div class="layui-form-label" style="width:90px;">查看 API 文档</div>
                            <div class="layui-input-inline" style="width:100px;">
                                <select asp-for="UseApiDoc" asp-items="new DictCommon().AdminUseApiDoc()"></select>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline" style="width:140px;">
                                <select asp-for="Status" asp-items="new DictCommon().AdminStatusItems()"></select>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-form-label">密码</div>
                            <div class="layui-input-inline" style="width:320px;">
                                <input type="text" asp-for="PassWord" lay-verify="required" autocomplete="off" placeholder="请填写密码"
                                       class="layui-input" />
                                <input type="checkbox" name="IsChangePassword" value="true" title="修改密码" />
                            </div>
                        </div>
                    </div>

                    <!-- 登录信息 -->
                    <div class="layui-tab-item">
                        @if (Model.Id > 0)
                        {
                            <div class="layui-form-item">
                                <div class="layui-form-label">登录次数</div>
                                <div class="layui-form-label content-label">
                                    @Model.LoginNum
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-form-label">最后<br />登录浏览器</div>
                                <div class="layui-form-label content-label">
                                    @Model.LoginCurrentBrower
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-form-label">最后登录 IP</div>
                                <div class="layui-form-label content-label">
                                    @Model.LoginCurrentIp
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-form-label">最后<br />登录时间</div>
                                <div class="layui-form-label content-label">
                                    @if (Model.LoginCurrentTime != DateTime.MinValue)
                                    {
                                        @Model.LoginCurrentTime
                                    }
                                </div>
                            </div>
                        }
                    </div>

                    <!-- 其它 -->
                    <div class="layui-tab-item">
                        @if (Model.Id > 0)
                        {
                            <div class="layui-form-item">
                                <div class="layui-form-label">创建时间</div>
                                <div class="layui-form-label content-label">
                                    @Model.CreateTime @Model.CreateUser
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-form-label">更新时间</div>
                                <div class="layui-form-label content-label">
                                    @Model.UpdateTime @Model.UpdateUser
                                </div>
                            </div>
                        }
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-fixbar btn-fixbar-box">
                <div class="layui-input-block">
                    <vc:menu-submit action-names="@(new string[]{"save"})"></vc:menu-submit>
                    <button type="button" class="layui-btn layui-btn-primary layui-border-red" lay-on="close">
                        <i class="layui-icon layui-icon-close"></i>关闭
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

@section Scripts {
    <script>
        layui.use(['form', 'jquery', 'xmSelect'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            var layer = layui.layer;

            createRoleTree();
            submitFn();

            // 创建 角色树
            function createRoleTree(){
                $.ajax({
                    url: '/role/alltree/',
                    type: 'get',
                    success: function (result) {
                        successFn(result);
                    }
                });

                function successFn(data) {
                    var $roleId = $('#roleId');

                    xmSelect.render({
                        el: '#roleClassTree',
                        radio: true,
                        height: '320px',
                        initValue: [$roleId.val()],    // 初始化选中值
                        prop: {
                            name: 'name',
                            value: 'id'
                        },
                        data: data,
                        tree: {
                            // 是否显示树状结构
                            show: true,
                            // 是否严格遵守父子模式
                            strict: false,
                            // 是否开启极简模式
                            simple: false
                        },
                        model: {
                            label: {
                                block: {
                                    showIcon: false // 隐藏删除按钮
                                }
                            }
                        },
                        // 选中事件
                        on: function (data) {
                            if (data.isAdd) {
                                $roleId.val(data.arr[0].id);
                            }
                        }
                    });
                }
            }

            // 表单提交
            function submitFn() {
                form.on('submit(save)', function (data) {

                   var layerIndex = layer.msg('正在保存', {
                      icon: 16,
                      shade: 0.1,
                      time: 0
                    });

                    $.ajax({
                        url: '/admin/save/',
                        type: 'post',
                        dataType: 'json',
                        data: data.field,
                        success: function (result) {
                            if (result.code === 200) {
                                layer.msg(result.message, { icon: 1, time: 1000 }, function () {
                                    parent.layer.close(parent.layer.getFrameIndex(window.name)); // 关闭当前页
                                    parent.layui.table.reload("tableList", {
                                        page: { curr: 1 },
                                    });
                                });
                            } else {
                                parent.layer.msg(result.message, { icon: 2 })
                            }
                        },
                        complete‌: function(){
                            layer.close(layerIndex);
                        }
                    });

                    return false;
                });
            }
        });
    </script>
}